﻿Namespace Control
    Public Class BuscarCep
        Implements IDisposable


        Private _logradouro As String
        Private _tipoLogradouro As String
        Private _complemento As String
        Private _bairro As String

        Private _municipio As Model.Municipio
        Private _estado As Model.Estado

        Private _municipios As IList(Of Model.Municipio)
        Private _estados As IList(Of Model.Estado)


        Public ReadOnly Property Logradouro() As String
            Get
                Return _tipoLogradouro + " " + _logradouro
            End Get
        End Property
        Public ReadOnly Property Complemento() As String
            Get
                Return _complemento
            End Get
        End Property
        Public ReadOnly Property Bairro() As String
            Get
                Return _bairro
            End Get
        End Property
        Public ReadOnly Property Municipios() As IList(Of Model.Municipio)
            Get
                Return _municipios
            End Get
        End Property
        Public ReadOnly Property municipio() As Model.Municipio
            Get
                Return _municipio
            End Get
        End Property

        Public ReadOnly Property Estados() As IList(Of Model.Estado)
            Get
                Return _estados
            End Get
        End Property

        Public ReadOnly Property estado() As Model.Estado
            Get
                Return _estado
            End Get
        End Property

        Public Function Buscar(ByVal cep As String) As Boolean
            Dim Result As Boolean

            Result = False

            Core.StatusMessage.Show("Buscando CEP Online...")

            Using BuscaCep As New Library.Util.BuscaCEP
                If BuscaCep.Buscar(cep) Then
                    Dim ibgeMunicipio As Integer
                    Dim ibgeEstado As Integer

                    Dim ctMunicipio As New Control.Municipio
                    Dim ctEstado As New Control.Estado

                    Result = True

                    _logradouro = BuscaCep.Logradouro
                    _bairro = BuscaCep.Bairro
                    _complemento = BuscaCep.Complemento

                    ibgeMunicipio = BuscaCep.IbgeMunicipioVerificador
                    ibgeEstado = BuscaCep.IbgeUF


                    _estados = ctEstado.ListaTudo()

                    ' Seta Estado
                    If ibgeEstado > 0 Then
                        'cbEstado.SelectedValue = 0
                        'cbEstado.SelectedItem = Nothing

                        'bsEstado.DataSource = ctEstado.GetByIBGE(ibgeEstado)
                        Dim Estado As Model.Estado = ctEstado.GetByIBGE(ibgeEstado)
                        If Estado IsNot Nothing Then
                            'cbEstado.SelectedValue = Estado.cd_estado
                            'cbEstado.SelectedItem = Estado
                            _estado = Estado

                            ' Traz somente os municípos do estado
                            _municipios = ctMunicipio.GetByEstado(Estado)

                            ' Seta Municipio
                            If ibgeMunicipio > 0 Then
                                'cbMunicipio.SelectedValue = 0
                                'cbMunicipio.SelectedItem = Nothing

                                Dim Municipio As Model.Municipio = ctMunicipio.GetByIBGE(ibgeMunicipio)
                                If Municipio IsNot Nothing Then
                                    'cbMunicipio.SelectedValue = Municipio.cd_municipio
                                    'cbMunicipio.SelectedItem = Municipio
                                    _municipio = Municipio
                                End If
                            End If

                        End If
                    End If

                    'bsCliente.EndEdit()

                Else
                    Core.StatusMessage.Show(BuscaCep.Retorno)
                End If
            End Using

            'Core.StatusMessage.Show("")

            Return Result
        End Function

        Private disposedValue As Boolean = False        ' To detect redundant calls

        ' IDisposable
        Protected Overridable Sub Dispose(ByVal disposing As Boolean)
            If Not Me.disposedValue Then
                If disposing Then
                    ' TODO: free other state (managed objects).
                End If

                ' TODO: free your own state (unmanaged objects).
                ' TODO: set large fields to null.
            End If
            Me.disposedValue = True
        End Sub

#Region " IDisposable Support "
        ' This code added by Visual Basic to correctly implement the disposable pattern.
        Public Sub Dispose() Implements IDisposable.Dispose
            ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
            Dispose(True)
            GC.SuppressFinalize(Me)
        End Sub
#End Region

    End Class
End Namespace